﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.Infastructure;
using System.Web;
using Toolkit.Web.ExtensionMethods;

namespace Toolkit.Web.ViewComponents.Button
{
    public sealed class ButtonBuilder : ViewComponentBuilderBase<Button>
    {
        private ButtonMenuBuilder _buttonMenuBuilder;

        public ButtonBuilder(Button component)
            : base(component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }

            _buttonMenuBuilder = new ButtonMenuBuilder(Component.Menu);
        }

        public ButtonBuilder Enable(bool isEnabled)
        {
            Component.IsEnabled = isEnabled;
            return this;
        }

        public ButtonBuilder Title(string value)
        {
            Component.Title = HtmlStringExtensions.Create(HttpUtility.HtmlEncode(value));
            return this;
        }

        public ButtonBuilder Title(IHtmlString value)
        {
            Component.Title = value;
            return this;
        }

        public ButtonBuilder Title(Action<IViewComponentWriter> template)
        {
            if (template != null)
            {
                Component.Title = template.ToHtmlString();
            }
            else
            {
                Component.Title = null;
            }

            return this;
        }

        public ButtonBuilder PrimaryIcon(ButtonDisplay display, object htmlAttributes)
        {
            Component.PrimaryIconDisplay = display;

            if (htmlAttributes != null)
            {
                Component.PrimaryIconHtmlAttributes.Merge(htmlAttributes, true);
            }

            return this;
        }

        public ButtonBuilder SecondaryIcon(ButtonDisplay display, object htmlAttributes)
        {
            Component.SecondaryIconDisplay = display;

            if (htmlAttributes != null)
            {
                Component.SecondaryIconHtmlAttributes.Merge(htmlAttributes, true);
            }

            return this;
        }

        public ButtonBuilder CustomMenu(Action<object, IViewComponentWriter> template)
        {
            if (template != null)
            {
                if (Component.Menu.OverrideTemplate == null)
                {
                    Component.Menu.OverrideTemplate = new HtmlTemplate();
                }

                Component.Menu.OverrideTemplate.ActionTemplate = template;
            }
            else
            {
                Component.Menu.OverrideTemplate = null;
            }

            return this;
        }

        public ButtonBuilder CustomMenu(Func<object, object> template)
        {
            if (template != null)
            {
                if (Component.Menu.OverrideTemplate == null)
                {
                    Component.Menu.OverrideTemplate = new HtmlTemplate();
                }

                Component.Menu.OverrideTemplate.InlineTemplate = template;
            }
            else
            {
                Component.Menu.OverrideTemplate = null;
            }

            return this;
        }

        public ButtonBuilder Menu(Action<ButtonMenuBuilder> action)
        {
            if (action != null)
            {
                Component.Menu.OverrideTemplate = null;
                action(_buttonMenuBuilder);
            }

            return this;
        }

        public ButtonBuilder ClientEventHandlers(Action<ClientEventHandlers> handlers)
        {
            if (handlers != null)
            {
                handlers(Component.ClientEventHandlers);
            }
            return this;
        }
    }
}
